package hash

import (
	"bytes"
	"crypto/md5"
	"crypto/sha1"
	"crypto/sha256"
	"crypto/sha512"
	"fmt"
	"io"
	"os"
)

func GetFileMD5(filePath string) string {
	file, err := os.Open(filePath)
	if err != nil {
		return ""
	}
	defer file.Close()

	md5Hash := md5.New()
	if _, err := io.Copy(md5Hash, file); err != nil {
		return ""
	}

	return fmt.Sprintf("%x", md5Hash.Sum(nil))
}

func GetFileSHA1(filePath string) string {
	file, err := os.Open(filePath)
	if err != nil {
		return ""
	}
	defer file.Close()

	sha1Hash := sha1.New()
	if _, err := io.Copy(sha1Hash, file); err != nil {
		return ""
	}
	return fmt.Sprintf("%x", sha1Hash.Sum(nil))
}

func GetFileSHA256(filePath string) string {
	file, err := os.Open(filePath)
	if err != nil {
		return ""
	}
	defer file.Close()

	sha256Hash := sha256.New()
	if _, err := io.Copy(sha256Hash, file); err != nil {
		return ""
	}
	return fmt.Sprintf("%x", sha256Hash.Sum(nil))
}

func GetFileSHA512(filePath string) string {
	file, err := os.Open(filePath)
	if err != nil {
		return ""
	}
	defer file.Close()

	sha512Hash := sha512.New()
	if _, err := io.Copy(sha512Hash, file); err != nil {
		return ""
	}
	return fmt.Sprintf("%x", sha512Hash.Sum(nil))
}

func GetFileMD5FileByte(filePath []byte) string {
	md5Hash := md5.New()
	if _, err := io.Copy(md5Hash, bytes.NewReader(filePath)); err != nil {
		return ""
	}
	return fmt.Sprintf("%x", md5Hash.Sum(nil))
}

func GetFileSHA1FileByte(filePath []byte) string {
	sha1Hash := sha1.New()
	if _, err := io.Copy(sha1Hash, bytes.NewReader(filePath)); err != nil {
		return ""
	}
	return fmt.Sprintf("%x", sha1Hash.Sum(nil))
}

func GetFileSHA256FileByte(filePath []byte) string {
	sha256Hash := sha256.New()
	if _, err := io.Copy(sha256Hash, bytes.NewReader(filePath)); err != nil {
		return ""
	}
	return fmt.Sprintf("%x", sha256Hash.Sum(nil))
}

func GetFileSHA512FileByte(filePath []byte) string {
	sha512Hash := sha512.New()
	if _, err := io.Copy(sha512Hash, bytes.NewReader(filePath)); err != nil {
		return ""
	}
	return fmt.Sprintf("%x", sha512Hash.Sum(nil))
}
